// 用户服务
import { apiClient, config } from './api'
import { API_ENDPOINTS } from '../config'

// 用户信息接口
export interface User {
  id: string
  name: string
  email: string
  role: string
  createdAt: string
  updatedAt: string
}

// 用户列表响应接口
export interface UserListResponse {
  users: User[]
  total: number
  page: number
  pageSize: number
}

// 登录请求接口
export interface LoginRequest {
  username: string
  password: string
}

// 登录响应接口
export interface LoginResponse {
  token: string
  user: User
}

// 用户服务
export const userService = {
  // 用户登录
  login: async (credentials: LoginRequest): Promise<LoginResponse> => {
    return await apiClient.post<LoginResponse>(
      API_ENDPOINTS.AUTH.LOGIN,
      credentials
    )
  },
  
  // 用户登出
  logout: async (): Promise<void> => {
    return await apiClient.post<void>(API_ENDPOINTS.AUTH.LOGOUT)
  },
  
  // 获取用户列表
  getUsers: async (page: number = 1, pageSize: number = config.pagination.defaultPageSize): Promise<UserListResponse> => {
    return await apiClient.get<UserListResponse>(API_ENDPOINTS.USER.LIST, {
      page,
      pageSize,
    })
  },
  
  // 获取用户详情
  getUser: async (id: string): Promise<User> => {
    return await apiClient.get<User>(`${API_ENDPOINTS.USER.LIST}/${id}`)
  },
  
  // 创建用户
  createUser: async (user: Omit<User, 'id' | 'createdAt' | 'updatedAt'>): Promise<User> => {
    return await apiClient.post<User>(API_ENDPOINTS.USER.CREATE, user)
  },
  
  // 更新用户
  updateUser: async (id: string, user: Partial<User>): Promise<User> => {
    return await apiClient.put<User>(`${API_ENDPOINTS.USER.UPDATE}/${id}`, user)
  },
  
  // 删除用户
  deleteUser: async (id: string): Promise<void> => {
    return await apiClient.delete<void>(`${API_ENDPOINTS.USER.DELETE}/${id}`)
  },
}

// 默认导出
export default userService